package demoMod.invincibleOne.cards.invincible;

import basemod.BaseMod;
import basemod.helpers.CardModifierManager;
import basemod.interfaces.OnPlayerTurnStartSubscriber;
import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.actions.common.ExhaustSpecificCardAction;
import com.megacrit.cardcrawl.actions.unique.RemoveAllPowersAction;
import com.megacrit.cardcrawl.cards.CardGroup;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.localization.CardStrings;
import com.megacrit.cardcrawl.monsters.AbstractMonster;
import demoMod.invincibleOne.InvincibleOneMod;
import demoMod.invincibleOne.enums.CardTagsEnum;
import demoMod.invincibleOne.modifiers.ExhaustAtStartOfCombatMod;
import demoMod.invincibleOne.relics.TaskGoal;
import demoMod.invincibleOne.skins.Skin;
import javafx.concurrent.Task;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

public class NoOneLives extends AbstractInvincibleCard implements OnPlayerTurnStartSubscriber {
    public static final String ID = InvincibleOneMod.makeID("NoOneLives");

    private static final CardStrings cardStrings = CardCrawlGame.languagePack.getCardStrings(ID);
    public static final String NAME = cardStrings.NAME;
    public static final String DESCRIPTION = cardStrings.DESCRIPTION;
    public static final String IMG_PATH = "cards/NoOneLives.png";

    private static final CardType TYPE = CardType.SKILL;
    private static final CardRarity RARITY = CardRarity.BASIC;
    private static final CardTarget TARGET = CardTarget.ALL_ENEMY;

    private static final int COST = 1;

    private int counter = 0;
    private int maxCount;

    public NoOneLives() {
        super(ID, NAME, InvincibleOneMod.getResourcePath(IMG_PATH), COST, DESCRIPTION, TYPE, RARITY, TARGET);
        this.baseMagicNumber = this.magicNumber = 7;
        this.selfRetain = true;
        this.modifier = new ExhaustAtStartOfCombatMod();
        CardModifierManager.addModifier(this, modifier);
    }

    @Override
    public Runnable getUpgradeAction() {
        return () -> {
            this.rawDescription = cardStrings.UPGRADE_DESCRIPTION;
            this.initializeDescription();
            upgradeMagicNumber(-1);
        };
    }

    @Override
    public void use(AbstractPlayer p, AbstractMonster m) {
        List<String> collection = Arrays.asList(Skin.TrashTalkKillAll);
        Collections.shuffle(collection, new Random(AbstractDungeon.shuffleRng.randomLong()));
        Skin.TrashTalk(collection.get(0), true);
        addToBot(new AbstractGameAction() {
            @Override
            public void update() {
                (AbstractDungeon.getCurrRoom()).cannotLose = false;
                for (AbstractMonster m2 : AbstractDungeon.getMonsters().monsters.stream().filter(m2 -> !m2.isDeadOrEscaped() || m2.halfDead).collect(Collectors.toList())) {
                    addToBot(new RemoveAllPowersAction(m2, false));
                    addToBot(new AbstractGameAction() {
                        @Override
                        public void update() {
                            m2.currentHealth = 0;
                            m2.healthBarUpdatedEvent();
                            m2.die();
                            isDone = true;
                        }
                    });
                }
                addToBot(new AbstractGameAction() {
                    @Override
                    public void update() {
                        AbstractDungeon.getCurrRoom().endBattle();
                    }
                });
                isDone = true;
            }
        });
    }

    public void onInitDeck() {
        AbstractDungeon.actionManager.addToTop(new ExhaustSpecificCardAction(this, AbstractDungeon.player.drawPile, true));
        BaseMod.unsubscribe(this);
        if (AbstractDungeon.player.drawPile.contains(this)) {
            maxCount = magicNumber;
            BaseMod.subscribe(this);
            maxCount += AbstractDungeon.player.masterDeck.group.stream().filter(c -> c instanceof BrokenWatch && !c.upgraded).count();
            maxCount += 2 * AbstractDungeon.player.masterDeck.group.stream().filter(c -> c instanceof BrokenWatch && c.upgraded).count();
        }
    }

    @Override
    public boolean onBattleStart() {
        return true;
    }

    @Override
    public void onRemoveFromMasterDeck() {
        CardModifierManager.removeSpecificModifier(this, modifier, true);
        BaseMod.unsubscribeLater(this);
    }

    @Override
    public void atEndOfTurn(CardGroup currGroup) {

    }

    @Override
    public void receiveOnPlayerTurnStart() {
        AbstractPlayer p = AbstractDungeon.player;
        if (p.exhaustPile.contains(this)) {
            counter++;
            if (counter >= this.maxCount) {
                addToBot(new AbstractGameAction() {
                    @Override
                    public void update() {
                        NoOneLives.this.unfadeOut();
                        p.hand.addToHand(NoOneLives.this);
                        if (AbstractDungeon.player.hasPower("Corruption") && NoOneLives.this.type == CardType.SKILL) {
                            NoOneLives.this.setCostForTurn(-9);
                        }

                        p.exhaustPile.removeCard(NoOneLives.this);
                        NoOneLives.this.unhover();
                        NoOneLives.this.fadingOut = false;
                        isDone = true;
                    }
                });
            }
        }
    }
}
